<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_mail_MailChecker {
    private $mails;
    private $domaine="codizer.org";
    private $checkerMail="noreply@codizer.org";

    function addMail($m)
    {
        $ret = preg_match("#^.*@(.*\.[a-z]{2,4})$#",$m,$t);
        if(!$ret)
            return -1;//premier check pas valide ...

        //on créer un sous tableau par domaine différent
        $this->mails[$t[1]][]=$m;
        return 1;
    }

    function getMails($dom="")
    {
        if(strlen($dom)>0)
            return $this->mails[$dom];
        return $this->mails;
    }

    function checkAllDomain()
    {
        $valid=array();
        foreach($this->mails as $dom => $tabMail)
        {
            $valid =array_merge($this->checkMailSameDomain($dom, $tabMail),$valid);
        }
        return $valid;
    }

    function checkMailSameDomain($domain,$tab)
    {
        $tabValid=array();
        $socket = socket_create(AF_INET, SOCK_STREAM, 0);
        $nbMail = count($tab);
        
        getmxrr($domain,$r);
        if(count($r)==0)
        {//try to connect directly to the server
            @fsockopen($domain,"25",$er1,$er2,0.5); //can't use it with socket :s
            if($er1>0)
                return -3;//no mx server and no smtp server
            $r[0]=$domain;
        }

        if(!socket_connect($socket,end($r),"25"))
            return -2;//can't connect

        socket_recv($socket,$retour,1024,0);
        $ret=f_mail_MailChecker::getCodeMsg($retour);
        if($ret[0]!=220)
            return -4;

        socket_write($socket,"EHLO ".$this->domaine."\n");
        socket_recv($socket,$retour,1024,0);
        $ret=f_mail_MailChecker::getCodeMsg($retour);

        if($ret[0]!=250)
            return -7;

        socket_write($socket,"MAIL FROM: <".$this->checkerMail.">\n");
        socket_recv($socket,$retour,1024,0);
        $ret=f_mail_MailChecker::getCodeMsg($retour);

        if($ret[0]!=250)
            return -5;

        for($i=0;$i<$nbMail;$i++)
        {
            socket_write($socket,"RCPT TO: <".$tab[$i].">\n");
            socket_recv($socket,$retour,1024,0);
            $ret =f_mail_MailChecker::getCodeMsg($retour);
            
            if($ret[0]==250)
                $tabValid[]=$tab[$i];
        }
        socket_write($socket,"quit\n");//bisou
        socket_close($socket);
        
        return $tabValid;
    }

    static function simpleMailTester($mail)
    {
        if(preg_match("#^(.*)@(.*\.[a-z]{2,4})$#i",$mail,$ret))
        {
            $socket = socket_create(AF_INET, SOCK_STREAM, 0);
            getmxrr($ret[2],$r);
            if(count($r)==0)
            {//try to connect directly to the server
                @fsockopen($ret[2],"25",$er1,$er2,0.5); //can't use it with socket :s
                if($er1>0)
                    return -3;//no mx server and no smtp server
                $r[0]=$ret[2];
            }

            if(!socket_connect($socket,$r[0],"25"))
                return -2;//can't connect

            socket_recv($socket,$retour,1024,0);
            $ret=f_mail_MailChecker::getCodeMsg($retour);
            if($ret[0]!=220)
                return -4;

            socket_write($socket,"EHLO advisa.fr\n");
            socket_recv($socket,$retour,1024,0);
            $ret=f_mail_MailChecker::getCodeMsg($retour);

            if($ret[0]!=250)
                return -7;

            socket_write($socket,"MAIL FROM: <noreply@codizer.org>\n");
            socket_recv($socket,$retour,1024,0);
            $ret=f_mail_MailChecker::getCodeMsg($retour);

            if($ret[0]!=250)
                return -5;

            socket_write($socket,"RCPT TO: <".$mail.">\n");
            socket_recv($socket,$retour,1024,0);
            socket_write($socket,"quit\n");//bisou
            $ret =f_mail_MailChecker::getCodeMsg($retour);
            socket_close($socket);
            if($ret[0]==250)
                return 1;
            else
                return -6;
        }
        return -1;//not an adress mail
    }
    static function getCodeMsg($ret)
    {
        preg_match("#^([0-9]+) (.*)$#im",$ret,$tab);
        array_shift($tab);
        return $tab;
    }
}
?>
